package Manage;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import Info.AdminI;
import Util.DatabaseConn;

public class AdminM {
	private Connection conn;
	private ResultSet rs;
	private PreparedStatement pstmt;
	public AdminM(){
		
	}
	public AdminI Login(String name){	   
		   new DatabaseConn();
		   AdminI a=null;
		   try{
		     conn=DatabaseConn.getConnection();
		     pstmt=conn.prepareStatement("select * from admin where name=?");	     
		     pstmt.setString(1,name);
		     rs=pstmt.executeQuery();
		     while(rs.next()){
		    	int id = rs.getInt("id");
		     	name = rs.getString("name");
		     	String password = rs.getString("password");
		     	a = new AdminI(id, name, password);
		     	}
		    }catch(SQLException e){
		     e.printStackTrace();
		    }finally{
		     DatabaseConn.closeConn(conn);
		    }
		     return a;
		  }
	
	public void Update(AdminI e) {
		Connection conn=null;
		PreparedStatement pstmt=null;			
		try {
			conn=DatabaseConn.getConnection();				
			pstmt=conn.prepareStatement("update admin set password=? where name=?");			
			pstmt.setString(1,e.getPassword());
			pstmt.setString(2,e.getName());
			pstmt.executeUpdate();
		} catch (SQLException e1) {
			e1.printStackTrace();
		}finally {
			DatabaseConn.closeConn(conn);
		}	
	}
}
